/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Util;
import Pojo.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author An
 */
public class DeThiDAO {
    public static boolean ThemDeThi(Capdo capdo, int thoigianlambai)
    {
        
        String md = All_Ham_XuLyCauHoi.RandomMaDe();
        int made = Integer.parseInt(md);
        LuuDSCauHoi_String_Pojo luuds = All_Ham_XuLyCauHoi.LuuDSCauHoi_String(capdo);
        
        Dethi dethi = new Dethi();
        dethi.setMaDeThi(made);
        dethi.setKho(luuds.getKho());
        dethi.setTrungBinh(luuds.getTrungbinh());
        dethi.setDe(luuds.getDe());
        dethi.setThoiGian(thoigianlambai);
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try{
            transaction = session.beginTransaction();
            session.save(dethi);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex.getMessages());
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
    
    public static Dethi LayThongTinDeThiTheoMaDe(int madethi)
    {
        Dethi dt = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            dt = (Dethi) session.get(Dethi.class, madethi);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return dt;
    }
}
